package com.googlecode.slm.web.springweb;

import java.util.List;

import org.json.JSONArray;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.googlecode.slm.domain.web.TreeNode;
import com.googlecode.slm.service.TreeService;
import com.googlecode.slm.util.CONSTANTS;
import com.googlecode.slm.web.BasicControler;

@Controller
@RequestMapping(value = "/treepopedom")
public class TreePopedomControler extends BasicControler {

	
    @Autowired
    @Qualifier(value = "treeService")
    private TreeService treeService;
    
    
	public TreeService getTreeService() {
		return treeService;
	}

	public void setTreeService(TreeService treeService) {
		this.treeService = treeService;
	}


	@RequestMapping(value = "/welcome.form")
	public String welcome(){
		return CONSTANTS.VIEWURL_TREEPOPEDOM;
	}
	
	/**
	 * 加载
	 * @param id
	 * @param gardid
	 * @return
	 */
	@RequestMapping(value = "/allTreeGardAjax.form")
	@ResponseBody
	public String getAllTreeGard(String id, int gardid){
		List<TreeNode> list = null;
    	if (id!=null&&!id.equals("")){
    		list = getTreeService().getUserGardTreeList(Integer.parseInt(id), gardid);
    	} else {
    		list = getTreeService().getUserGardTreeList(0, gardid);
    	}
        JSONArray array = new JSONArray(list, false);
        return array.toString();
	}
	
	/**
	 * 保存
	 * @param fid
	 * @param gardid
	 * @return
	 */
	@RequestMapping(value = "/saveAjax.form")
	@ResponseBody
	public String save(int[] fid, int gardid){
		if (getTreeService().saveGardTree(fid, gardid))
			return "成功";
		else
			return "错误";
	}
}
